#include "stationpoint.h"
#include "ui_stationpoint.h"
#include <QDebug>

StationPoint::StationPoint(int staSeq, QString name, QPointF _pos, QCustomPlot *_plot, QWidget *parent) :
    QPushButton(parent),
    ui(new Ui::StationPoint)
{
    ui->setupUi(this);
    this->resize(20,20);
    stationIndex = staSeq;
    info = new StationInfo(name, parent);
//    if(plot == NULL)
//        return NULL;
    passed = false;
    checkLock = false;
    pos = _pos;
    plot = _plot;
    updatePosition();
//    qDebug()<<"coor"<<pos.x()<<pos.y();
//    qDebug()<<"pix"<<plot->xAxis->coordToPixel(pos.x())<<plot->yAxis->coordToPixel(pos.y());
//    qDebug()<<plot->xAxis->pixelToCoord(50);
//    this->move(plot->xAxis->coordToPixel(pos.x()), plot->yAxis->coordToPixel(pos.y()));
    this->show();
    connect(plot, SIGNAL(afterReplot()), this, SLOT(updatePosition()));
    connect(info, SIGNAL(go(bool)), this, SLOT(goOrNot(bool)));
}

StationPoint::~StationPoint()
{
    delete ui;
}

QPointF StationPoint::getPos()
{
    return pos;
}

bool StationPoint::getState()
{
    return passed;
}

void StationPoint::setState(bool state)
{
    passed = state;
    checkLock = state;
    this->setEnabled(!state);
    this->setChecked(false);
}

void StationPoint::setIndex(int index)
{
    _index = index;
    qDebug()<<"[setIndex]"<<index;
}

int StationPoint::mapIndex()
{
    return _index;
}

void StationPoint::updatePosition()
{
    this->move(plot->xAxis->coordToPixel(pos.x())-10, plot->yAxis->coordToPixel(pos.y())-10);
    info->move(this->geometry().topRight());
    //    qDebug()<<"station"<<(plot->xAxis->coordToPixel(pos.x())-10)<<(plot->yAxis->coordToPixel(pos.y())-10);
}

void StationPoint::goOrNot(bool go)
{
    if(go)
    {
        this->setAutoExclusive(false);
        emit gotoPos(pos, stationIndex);
        checkLock = true;
//        qDebug()<<"go"<<pos;
    }
    else
    {
        this->setAutoExclusive(false);
        this->setChecked(false);
        this->setAutoExclusive(true);
        qDebug()<<this->isChecked();
    }
}

void StationPoint::on_StationPoint_clicked()
{

}

void StationPoint::on_StationPoint_toggled(bool checked)
{
    info->setExpand(checked);
}

void StationPoint::nextCheckState()
{
    if((!checkLock) && isCheckable())
    {
        setChecked(!isChecked());
    }
}
